/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.relatorio;

import br.com.deployment.daoImpl.DeterminationDAOImpl;
import br.com.deployment.daoImpl.ResponseDAOImpl;
import br.com.deployment.entities.Determinations;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.QuestionnaireResponse;
import br.com.deployment.entities.Response;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerRelDeterminations {

    @EJB
    ResponseDAOImpl responseService;
    @EJB
    DeterminationDAOImpl determinationService;
    
    // Objetos
    private QuestionnaireResponse quesResponse;
    
    // Lista de Objetos
    private List<Response> responses;
    private List<Determinations> lstDeterminations;
    private List<Determinations> lstD;

    public ManagerRelDeterminations() {
    }

    public void generetorReportPdf() throws JRException, IOException {

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date data = quesResponse.getDateQ(); 
        String texto="";
        
        texto +="Tendo em vista a Inspeção realizada no período de "+ df.format(data);
        texto +=" no "+quesResponse.getNotaryOffice().getNome()+ "da comarca de "+quesResponse.getNotaryOffice().getAddress().getCity().getName();
        texto +=" - "+quesResponse.getNotaryOffice().getAddress().getCity().getStates().getSgl()+" em virtude da Correicao "+quesResponse.getType();
        texto +=" nos moldes do provimento n˚ 006/2009 da Corregedoria Geral da Justiça, determino a oficial da indicada Serventia que no prazo indicado";
        texto +=" realize as determinações abaixo elencadas :";
        HashMap<String, Object> h = new HashMap<String, Object>();
        
        
        h.put("texto" , texto);
        
        h.put("cartorio" , quesResponse.getNotaryOffice().getNome());
        
        h.put("juiz"     , quesResponse.getExecutor().getName());
        
        lstD = new ArrayList<Determinations>();
//        responses = responseService.findResponseQResponse(this.quesResponse);
//        for(Response r : responses)
//        {
            lstDeterminations = determinationService.findDeterminations(null,this.quesResponse);
//            r.setDetermination(lstDeterminations);
//            for(Determinations d : r.getDetermination()){
//                   lstD.add(d);
//            }                
//        }     

        JRBeanCollectionDataSource collecion = new JRBeanCollectionDataSource(lstDeterminations);
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/relatorio/relDetermination.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, h, collecion);
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=determinação.pdf");
        ServletOutputStream serveOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, serveOutputStream);
        FacesContext.getCurrentInstance().responseComplete();

    }

   
    public void viewDetermination(QuestionnaireResponse q) {
//        try {
//            FacesContext context = FacesContext.getCurrentInstance();
            this.quesResponse = q;
//            responses = responseService.findResponseQResponse(this.quesResponse);
//            this.quesResponse.setResponse(responses);
//            for (Response r : quesResponse.getResponse()) {
                lstDeterminations = determinationService.findDeterminations(null, this.quesResponse);
//                r.setDetermination(lstDeterminations);                
//            }
            
//            context.getExternalContext().redirect("determinations.xhtml");
//        } catch (IOException ex) {
//            Logger.getLogger(ManagerEditQuestionnaireResponse.class
//                    .getName()).log(Level.SEVERE, null, ex);
//        }
    }
    
    public String issuesFromDetermination(Determinations d){
        return (determinationService.findIssuesDeterminations(d)).getTitle();
    }

     public List<Response> getResponses() {
        return responses;
    }

    public void setResponses(List<Response> responses) {
        this.responses = responses;
    }

    
    public QuestionnaireResponse getQuesResponse() {
        return quesResponse;
    }

    public void setQuesResponse(QuestionnaireResponse quesResponse) {
        this.quesResponse = quesResponse;
    }

    public List<Determinations> getLstDeterminations() {
        return lstDeterminations;
    }

    public void setLstDeterminations(List<Determinations> lstDeterminations) {
        this.lstDeterminations = lstDeterminations;
    }
}
